package com.hfzy.ihk.web.oauth.client.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.hfzy.ihk.facade.userCenter.entity.UserHasCallLine;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

import java.util.*;

/**
 * 用户工具类
 */
public class UserUtil {

	/**
	 * 获取当前登录用户
	 * @return
	 */
	public static UserInfo getLoginUser() {
		try {
			OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
			if (authentication != null && authentication.getUserAuthentication() != null) {
				LinkedHashMap hashMap = (LinkedHashMap) authentication.getUserAuthentication().getPrincipal();
				if(hashMap==null)
					return null;
				LinkedHashMap userPrincipal = (LinkedHashMap) hashMap.get("principal");
				if(userPrincipal ==null)
					return null;
				HashMap<String,Object> map = (LinkedHashMap<String,Object>) userPrincipal.get("userInfo");
				//UserInfo userInfo = BeanUtils.mapToBean(map,UserInfo.class);
				try{
					String jsonString = JSON.toJSONString(map);
					UserInfo userInfo = JSON.parseObject(jsonString,UserInfo.class);
					return userInfo;
				}catch (Exception e){
					return null;
				}
				/*UserInfo userInfo = new UserInfo();
				userInfo.setId(Integer.valueOf(map.get("id").toString()));
				userInfo.setEnabled(true);
				userInfo.setName(String.valueOf(map.get("name")));
				userInfo.setHeadImgUrl(String.valueOf(map.get("headImgUrl")));
				userInfo.setUsername(String.valueOf(map.get("username")));
				if(map.get("createUserId")!=null)
					userInfo.setCreateUserId(Integer.valueOf(map.get("createUserId").toString()));
				if(userInfo!=null&&userInfo.getId()!=null){
					ArrayList<LinkedHashMap> userHasCallLineMaps = (ArrayList<LinkedHashMap>) map.get("userHasCallLines");
					try{
						String jsonString = JSON.toJSONString(userHasCallLineMaps);
						List<UserHasCallLine> userHasCallLineList = JSONArray.parseArray(jsonString, UserHasCallLine.class);
						if(userHasCallLineList!=null && !userHasCallLineList.isEmpty()){
							userInfo.setUserHasCallLines(userHasCallLineList);
						}
					}catch (Exception e){//异常则不存入，说明数据有问题
					}
					return userInfo;
				}
				else
					return null;*/
			}
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
		return null;
	}

	public static String getLoginclientId() {
		String clientId=null;
		OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
		if (authentication != null && authentication.getUserAuthentication() != null) {
			if(authentication.getOAuth2Request()!=null&&authentication.getOAuth2Request().getClientId()!=null){
				clientId = authentication.getOAuth2Request().getClientId();
			}

		}
		return clientId;
	}


	public static String getToken(){
		try {
			OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
			if (authentication != null && authentication.getUserAuthentication() != null) {
				OAuth2AuthenticationDetails obj = (OAuth2AuthenticationDetails)authentication.getDetails();
				return obj!=null?obj.getTokenValue():"";
			}
		}catch (Exception e){

		}
		return "";
	}

	public static String getClickId(){
		OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
		if(authentication!=null && authentication.getOAuth2Request()!=null){
			return authentication.getOAuth2Request().getClientId();
		}
		return "";
	}


}